package com.example.sunlight.unicomsensing.View;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.unicomsensing.R;

import java.util.Random;

/**
 * Created by SunLight on 2017/9/15.
 */

public class CarMoveView extends View {
    Bitmap car, wen, wen_down, wen_light, msg_left, msg_right;
    Resources resources;
    MoveThread moveThread;
    SizeChange sizeChange;
    NumRandomThread numRandomThread;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    CarMoveView.this.invalidate();
                    break;
            }
        }
    };

    public CarMoveView(Context context) {
        super(context);
        init();

    }

    public void init() {
        resources = getResources();
        car = ((BitmapDrawable) resources.getDrawable(R.drawable.three)).getBitmap();
        wen = ((BitmapDrawable) resources.getDrawable(R.drawable.bowen)).getBitmap();
        wen_down = ((BitmapDrawable) resources.getDrawable(R.drawable.wen_down)).getBitmap();
        wen_light = ((BitmapDrawable) resources.getDrawable(R.drawable.wenlight)).getBitmap();
        msg_left = ((BitmapDrawable) resources.getDrawable(R.drawable.msg_left)).getBitmap();
        msg_right = ((BitmapDrawable) resources.getDrawable(R.drawable.msg_right)).getBitmap();
        moveThread = new MoveThread();
        moveThread.start();
        sizeChange = new SizeChange();
        sizeChange.start();
        numRandomThread = new NumRandomThread();
        numRandomThread.start();
        kit1 = Color.argb(200, 255, 0, 12);
        kit = Color.argb(100, 255, 0, 12);
        kit2 = Color.argb(200, 0, 255, 248);
    }

    public CarMoveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CarMoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(car, 605, 200 + movesize, null);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(20);
        if (movesize >= 100) {
            paint.setColor(kit1);
            canvas.drawCircle(615, 330 + movesize, 5, paint);
            canvas.drawCircle(740, 330 + movesize, 5, paint);
            canvas.drawCircle(680, 490 + movesize, 5, paint);

            paint.setColor(kit);
            canvas.drawCircle(615, 330 + movesize, 5 + size, paint);
            canvas.drawCircle(740, 330 + movesize, 5 + size, paint);
            canvas.drawCircle(680, 490 + movesize, 5 + size, paint);
            canvas.drawBitmap(wen, 745, 295 + movesize, null);
            canvas.drawBitmap(wen_down, 640, 495 + movesize, null);
            canvas.drawBitmap(wen_light, 570, 295 + movesize, null);
            canvas.drawBitmap(msg_right, 300, 200, null);
            canvas.drawBitmap(msg_left, 800, 200, null);
            paint.setColor(kit2);
            paint.setTextSize(30);
            canvas.drawText(Num + "", 325, 255, paint);
            canvas.drawText(Num2 + "", 940, 255, paint);
        }

    }

    int Num, Num2;

    class NumRandomThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    Num = new Random().nextInt(90) + 10;
                    Num2 = new Random().nextInt(90) + 10;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    int kit1, kit, kit2;
    boolean isState = true;
    int size;

    class SizeChange extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(70);
                    if (isState) {
                        if (size <= 10) {
                            size++;
                        } else {
                            isState = false;
                        }
                    } else {
                        if (size >= 0) {
                            size--;
                        } else {
                            isState = true;
                        }
                    }


                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    int movesize;
    boolean state = false;

    class MoveThread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(45);
                    if (state == false) {
                        if (movesize < 210) {
                            movesize++;
                        } else if (movesize == 210) {
                            Thread.sleep(3000);
                            state = true;
                        }
                    } else {
                        if (movesize >= 0) {
                            movesize--;
                        } else {
                            state = false;
                        }
                    }
                    handler.sendEmptyMessage(0x123);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
